<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>S110_nRF51822/Source/ble/ble_services/ble_bas.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="alias.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="alias.js"></script>

<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>


<script type="text/javascript">
  jQuery(document).ready(function () {
    if(gref){ // Number all _img and _table classes
      gref();
    }
  });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nRF51822 BLE SDK
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('ble__bas_8c.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">S110_nRF51822/Source/ble/ble_services/ble_bas.c</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.</span>
<a name="l00002"></a>00002 <span class="comment"> *</span>
<a name="l00003"></a>00003 <span class="comment"> * The information contained herein is property of Nordic Semiconductor ASA.</span>
<a name="l00004"></a>00004 <span class="comment"> * Terms and conditions of usage are described in detail in NORDIC</span>
<a name="l00005"></a>00005 <span class="comment"> * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Licensees are granted free, non-transferable use of the information. NO</span>
<a name="l00008"></a>00008 <span class="comment"> * WARRANTY of ANY KIND is provided. This heading must NOT be removed from</span>
<a name="l00009"></a>00009 <span class="comment"> * the file.</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * $LastChangedRevision$</span>
<a name="l00012"></a>00012 <span class="comment"> */</span>
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="ble__bas_8h.html">ble_bas.h</a>&quot;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &quot;<a class="code" href="nordic__common_8h.html" title="Common defines and macros for firmware developed by Nordic Semiconductor.">nordic_common.h</a>&quot;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;<a class="code" href="ble__srv__common_8h.html">ble_srv_common.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="ble__sdk__util_8h.html">ble_sdk_util.h</a>&quot;</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 
<a name="l00021"></a>00021 <span class="preprocessor">#define INVALID_BATTERY_LEVEL  255</span>
<a name="l00022"></a>00022 <span class="preprocessor"></span>
<a name="l00023"></a>00023 
<a name="l00024"></a>00024 <span class="keyword">static</span> <span class="keywordtype">void</span> on_connect(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00025"></a>00025 {
<a name="l00026"></a>00026     p_bas-&gt;<a class="code" href="structble__bas__s.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a> = p_ble_evt-&gt;evt.<a class="code" href="structble__evt__t.html#a91857ca158f0c4e43f610068772c7d3d">gatts_evt</a>.<a class="code" href="structble__gatts__evt__t.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a>;
<a name="l00027"></a>00027 }
<a name="l00028"></a>00028 
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 <span class="keyword">static</span> <span class="keywordtype">void</span> on_disconnect(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00031"></a>00031 {
<a name="l00032"></a>00032     UNUSED_PARAMETER(p_ble_evt);
<a name="l00033"></a>00033     p_bas-&gt;<a class="code" href="structble__bas__s.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a> = <a class="code" href="group___b_l_e___c_o_n_n___h_a_n_d_l_e_s.html#gacf227b9b101dbcf08eccbbaba54e48f5">BLE_CONN_HANDLE_INVALID</a>;
<a name="l00034"></a>00034 }
<a name="l00035"></a>00035 
<a name="l00036"></a>00036 
<a name="l00037"></a>00037 <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="group__ble__sdk__app__pwr__mgmt__main.html#ga70525a816fec301fa4c9d564831c440b" title="Funtion that evaluates the value written in CCCD.">on_write</a>(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039     <span class="keywordflow">if</span> (p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a>)
<a name="l00040"></a>00040     {
<a name="l00041"></a>00041         <a class="code" href="structble__gatts__evt__write__t.html" title="Event structure for BLE_GATTS_EVT_WRITE.">ble_gatts_evt_write_t</a> * p_evt_write = &amp;p_ble_evt-&gt;evt.<a class="code" href="structble__evt__t.html#a91857ca158f0c4e43f610068772c7d3d">gatts_evt</a>.params.<a class="code" href="structble__gatts__evt__t.html#a682c515271ef8a1b93b7643a1023e8b8">write</a>;
<a name="l00042"></a>00042         
<a name="l00043"></a>00043         <span class="keywordflow">if</span> (
<a name="l00044"></a>00044             (p_evt_write-&gt;<a class="code" href="structble__gatts__evt__write__t.html#af242d6e3b6108ea75e1eb236e94c8240">handle</a> == p_bas-&gt;<a class="code" href="structble__bas__s.html#a554415e857e1b95eafd8cd7bc70d88e9">battery_level_handles</a>.<a class="code" href="structble__gatts__char__handles__t.html#a2c7796d4eb58c25a1a724bcd9d5bf183">cccd_handle</a>)
<a name="l00045"></a>00045             &amp;&amp;
<a name="l00046"></a>00046             (p_evt_write-&gt;<a class="code" href="structble__gatts__evt__write__t.html#a8aed22e2c7b283705ec82e0120515618">len</a> == 2)
<a name="l00047"></a>00047         )
<a name="l00048"></a>00048         {
<a name="l00049"></a>00049             <span class="comment">// CCCD written, call application event handler</span>
<a name="l00050"></a>00050             <span class="keywordflow">if</span> (p_bas-&gt;<a class="code" href="structble__bas__s.html#a3c25f96941dd8c1b92407ed2994691c6">evt_handler</a> != NULL)
<a name="l00051"></a>00051             {
<a name="l00052"></a>00052                 <a class="code" href="structble__bas__evt__t.html" title="Battery Service event.">ble_bas_evt_t</a> evt;
<a name="l00053"></a>00053                 
<a name="l00054"></a>00054                 <span class="keywordflow">if</span> (<a class="code" href="group__ble__sdk__srv__common.html#ga11fd3cc1b15d4bec4accc37618305ccb" title="Inline function for decoding a CCCD value, and then testing if notification is enabled.">ble_srv_is_notification_enabled</a>(p_evt_write-&gt;<a class="code" href="structble__gatts__evt__write__t.html#aa86e1085c6a16f60258f29c9d5fc2144">data</a>))
<a name="l00055"></a>00055                 {
<a name="l00056"></a>00056                     evt.<a class="code" href="structble__bas__evt__t.html#a54dbfc0f61e2602953ac085fa8fb15a9">evt_type</a> = <a class="code" href="group__ble__sdk__srv__bas.html#ggab63b7f474ce48636cd355f6b75ea1ca9a657f8681ada82fd2990d5458442ed85a">BLE_BAS_EVT_NOTIFICATION_ENABLED</a>;
<a name="l00057"></a>00057                 }
<a name="l00058"></a>00058                 <span class="keywordflow">else</span>
<a name="l00059"></a>00059                 {
<a name="l00060"></a>00060                     evt.<a class="code" href="structble__bas__evt__t.html#a54dbfc0f61e2602953ac085fa8fb15a9">evt_type</a> = <a class="code" href="group__ble__sdk__srv__bas.html#ggab63b7f474ce48636cd355f6b75ea1ca9afcba4bddb1a583374bb27d3f3ed5b8d1">BLE_BAS_EVT_NOTIFICATION_DISABLED</a>;
<a name="l00061"></a>00061                 }
<a name="l00062"></a>00062 
<a name="l00063"></a>00063                 p_bas-&gt;<a class="code" href="structble__bas__s.html#a3c25f96941dd8c1b92407ed2994691c6">evt_handler</a>(p_bas, &amp;evt);
<a name="l00064"></a>00064             }
<a name="l00065"></a>00065         }
<a name="l00066"></a>00066     }
<a name="l00067"></a>00067 }
<a name="l00068"></a>00068 
<a name="l00069"></a>00069 
<a name="l00070"></a><a class="code" href="group__ble__sdk__srv__bas.html#ga01b70d77540660d7e312121d9f1d3b3a">00070</a> <span class="keywordtype">void</span> <a class="code" href="group__ble__sdk__srv__bas.html#ga01b70d77540660d7e312121d9f1d3b3a" title="Battery Service BLE stack event handler.">ble_bas_on_ble_evt</a>(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <a class="code" href="structble__evt__t.html" title="Common BLE Event type, wrapping the module specific event reports.">ble_evt_t</a> * p_ble_evt)
<a name="l00071"></a>00071 {
<a name="l00072"></a>00072     <span class="keywordflow">switch</span> (p_ble_evt-&gt;<a class="code" href="structble__evt__t.html#a50f33abba5627a26728ba30fffd9880a">header</a>.<a class="code" href="structble__evt__hdr__t.html#a826314e7ea25d4267621899efe6859f9">evt_id</a>)
<a name="l00073"></a>00073     {
<a name="l00074"></a>00074         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035daa0b4789724d202a13a5e7eab85c52957">BLE_GAP_EVT_CONNECTED</a>:
<a name="l00075"></a>00075             on_connect(p_bas, p_ble_evt);
<a name="l00076"></a>00076             <span class="keywordflow">break</span>;
<a name="l00077"></a>00077             
<a name="l00078"></a>00078         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_p.html#ggabc5c98fcc1211af2b80116dd6e0a035da057001bbd6ea9e615f19ef93cc0831ee">BLE_GAP_EVT_DISCONNECTED</a>:
<a name="l00079"></a>00079             on_disconnect(p_bas, p_ble_evt);
<a name="l00080"></a>00080             <span class="keywordflow">break</span>;
<a name="l00081"></a>00081             
<a name="l00082"></a>00082         <span class="keywordflow">case</span> <a class="code" href="group___b_l_e___g_a_t_t_s.html#gga80155586fa275b28773c9b203f52cabaaa68b7764f5264ff092d2ad17851355ed">BLE_GATTS_EVT_WRITE</a>:
<a name="l00083"></a>00083             <a class="code" href="group__ble__sdk__app__pwr__mgmt__main.html#ga70525a816fec301fa4c9d564831c440b" title="Funtion that evaluates the value written in CCCD.">on_write</a>(p_bas, p_ble_evt);
<a name="l00084"></a>00084             <span class="keywordflow">break</span>;
<a name="l00085"></a>00085             
<a name="l00086"></a>00086         <span class="keywordflow">default</span>:
<a name="l00087"></a>00087             <span class="keywordflow">break</span>;
<a name="l00088"></a>00088     }
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090 
<a name="l00091"></a>00091 
<a name="l00092"></a>00092 <span class="keyword">static</span> uint32_t battery_level_char_add(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <span class="keyword">const</span> <a class="code" href="structble__bas__init__t.html" title="Battery Service init structure. This contains all options and data needed for initialization of the s...">ble_bas_init_t</a> * p_bas_init)
<a name="l00093"></a>00093 {
<a name="l00094"></a>00094     uint32_t            err_code;
<a name="l00095"></a>00095     <a class="code" href="structble__gatts__char__md__t.html" title="GATT Characteristic Medatadata.">ble_gatts_char_md_t</a> char_md;
<a name="l00096"></a>00096     <a class="code" href="structble__gatts__attr__md__t.html" title="Attribute metadata.">ble_gatts_attr_md_t</a> cccd_md;
<a name="l00097"></a>00097     <a class="code" href="structble__gatts__attr__t.html" title="GATT Attribute.">ble_gatts_attr_t</a>    attr_char_value;
<a name="l00098"></a>00098     <a class="code" href="structble__uuid__t.html" title="Bluetooth Low Energy UUID type, encapsulates both 16-bit and 128-bit UUIDs.">ble_uuid_t</a>          ble_uuid;
<a name="l00099"></a>00099     <a class="code" href="structble__gatts__attr__md__t.html" title="Attribute metadata.">ble_gatts_attr_md_t</a> attr_md;
<a name="l00100"></a>00100     uint8_t             initial_battery_level;
<a name="l00101"></a>00101     uint8_t             encoded_report_ref[<a class="code" href="group__ble__sdk__srv__common.html#gafbfc0ef111ca725cc8bb500fd93301cc">BLE_SRV_ENCODED_REPORT_REF_LEN</a>];
<a name="l00102"></a>00102     uint8_t             init_len;
<a name="l00103"></a>00103     
<a name="l00104"></a>00104     <span class="comment">// Add Battery Level characteristic</span>
<a name="l00105"></a>00105     <span class="keywordflow">if</span> (p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a>)
<a name="l00106"></a>00106     {
<a name="l00107"></a>00107         memset(&amp;cccd_md, 0, <span class="keyword">sizeof</span>(cccd_md));
<a name="l00108"></a>00108     
<a name="l00109"></a>00109         <span class="comment">// According to BAS_SPEC_V10, the read operation on cccd should be possible without</span>
<a name="l00110"></a>00110         <span class="comment">// authentication.</span>
<a name="l00111"></a>00111         <a class="code" href="group___b_l_e___g_a_p___c_o_n_n___s_e_c___m_o_d_e___s_e_t___m_a_c_r_o_s.html#ga2abd1c711d49a4d4b1ab9bf27cafffbb" title="Set sec_mode pointed to by ptr to require no protection, open link.">BLE_GAP_CONN_SEC_MODE_SET_OPEN</a>(&amp;cccd_md.<a class="code" href="structble__gatts__attr__md__t.html#af0352a482ca2a1305ec5941bc4af0ec2">read_perm</a>);
<a name="l00112"></a>00112         cccd_md.<a class="code" href="structble__gatts__attr__md__t.html#a6060006e42afe1970aed0a8490f71445">write_perm</a> = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ada6a205c81f51d9aa2de33bbdea0bd26">battery_level_char_attr_md</a>.cccd_write_perm;
<a name="l00113"></a>00113     }
<a name="l00114"></a>00114     
<a name="l00115"></a>00115     memset(&amp;char_md, 0, <span class="keyword">sizeof</span>(char_md));
<a name="l00116"></a>00116     
<a name="l00117"></a>00117     char_md.<a class="code" href="structble__gatts__char__md__t.html#a5d81476b727bf4053879046f72049b48">char_properties</a>.<a class="code" href="structble__gatts__char__properties__t.html#ad99a3f990d972bf0b87f25fd507eb599">read</a>   = 1;
<a name="l00118"></a>00118     char_md.<a class="code" href="structble__gatts__char__md__t.html#a5d81476b727bf4053879046f72049b48">char_properties</a>.<a class="code" href="structble__gatts__char__properties__t.html#ab4ddab4f7ba8461e378ffa590bb15dd8">notify</a> = (p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a>) ? 1 : 0;
<a name="l00119"></a>00119     char_md.<a class="code" href="structble__gatts__char__md__t.html#a48ddd998bdda2b2b93e305ba46137d41">p_char_user_desc</a>       = NULL;
<a name="l00120"></a>00120     char_md.<a class="code" href="structble__gatts__char__md__t.html#ad1fe5fdc8f07028e11054f2288c7fc14">p_char_pf</a>              = NULL;
<a name="l00121"></a>00121     char_md.<a class="code" href="structble__gatts__char__md__t.html#a2bce0c863bf96476ad1629c676042d34">p_user_desc_md</a>         = NULL;
<a name="l00122"></a>00122     char_md.<a class="code" href="structble__gatts__char__md__t.html#a5d654340f27d7c8f113a45beebb50312">p_cccd_md</a>              = (p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a>) ? &amp;cccd_md : NULL;
<a name="l00123"></a>00123     char_md.<a class="code" href="structble__gatts__char__md__t.html#a642bc361c2131bdf054da18ce4e9a3e7">p_sccd_md</a>              = NULL;
<a name="l00124"></a>00124     
<a name="l00125"></a>00125     <a class="code" href="group__ble__types.html#gac4478362651889d2ade8030f53713588" title="Set .type and .uuid fields of ble_uuid_struct to specified uuid value.">BLE_UUID_BLE_ASSIGN</a>(ble_uuid, <a class="code" href="group___u_u_i_d___c_h_a_r_a_c_t_e_r_i_s_t_i_c_s.html#ga2d4368e048443fcb6d5ec5b11fe227c8">BLE_UUID_BATTERY_LEVEL_CHAR</a>);
<a name="l00126"></a>00126     
<a name="l00127"></a>00127     memset(&amp;attr_md, 0, <span class="keyword">sizeof</span>(attr_md));
<a name="l00128"></a>00128 
<a name="l00129"></a>00129     attr_md.<a class="code" href="structble__gatts__attr__md__t.html#af0352a482ca2a1305ec5941bc4af0ec2">read_perm</a>  = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ada6a205c81f51d9aa2de33bbdea0bd26">battery_level_char_attr_md</a>.<a class="code" href="structble__srv__cccd__security__mode__t.html#af0352a482ca2a1305ec5941bc4af0ec2">read_perm</a>;
<a name="l00130"></a>00130     attr_md.<a class="code" href="structble__gatts__attr__md__t.html#a6060006e42afe1970aed0a8490f71445">write_perm</a> = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ada6a205c81f51d9aa2de33bbdea0bd26">battery_level_char_attr_md</a>.<a class="code" href="structble__srv__cccd__security__mode__t.html#a6060006e42afe1970aed0a8490f71445">write_perm</a>;
<a name="l00131"></a>00131     attr_md.<a class="code" href="structble__gatts__attr__md__t.html#ac68fa2d9eb5f49771a80414d2c6bb690">rd_defer</a>   = 0;
<a name="l00132"></a>00132     attr_md.<a class="code" href="structble__gatts__attr__md__t.html#ad44d77b2d08d2dde61c9ccbb22fa7432">wr_defer</a>   = 0;
<a name="l00133"></a>00133     attr_md.<a class="code" href="structble__gatts__attr__md__t.html#a248864ad6fe6949aa81d376cdd7d07b4">vlen</a>       = 0;
<a name="l00134"></a>00134     
<a name="l00135"></a>00135     initial_battery_level = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ae5e656df4b847b3a099792ae32676d16">initial_batt_level</a>;
<a name="l00136"></a>00136     
<a name="l00137"></a>00137     memset(&amp;attr_char_value, 0, <span class="keyword">sizeof</span>(attr_char_value));
<a name="l00138"></a>00138 
<a name="l00139"></a>00139     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a8af1eafb75d1d1e9c4cacf188fad299e">p_uuid</a>       = &amp;ble_uuid;
<a name="l00140"></a>00140     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a573caddb6f66206646fcf38fa7c1a211">p_attr_md</a>    = &amp;attr_md;
<a name="l00141"></a>00141     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a9e68f6d248f0c128406979df16f88255">init_len</a>     = <span class="keyword">sizeof</span>(uint8_t);
<a name="l00142"></a>00142     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a44cd93c5a44c06d426920c36a3971eb1">init_offs</a>    = 0;
<a name="l00143"></a>00143     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a1a6c02276292f164788906e8d5a316e5">max_len</a>      = <span class="keyword">sizeof</span>(uint8_t);
<a name="l00144"></a>00144     attr_char_value.<a class="code" href="structble__gatts__attr__t.html#ac439d398694461e592c0af528994705e">p_init_value</a> = &amp;initial_battery_level;
<a name="l00145"></a>00145     
<a name="l00146"></a>00146     err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#gae4a1d9149f4531d66d06bf289886106d" title="Add a characteristic declaration, a characteristic value declaration and optional characteristic desc...">ble_gatts_characteristic_add</a>(p_bas-&gt;<a class="code" href="structble__bas__s.html#a363e00d9262febd8d752ed2f933be1e4">service_handle</a>, &amp;char_md,
<a name="l00147"></a>00147                                             &amp;attr_char_value,
<a name="l00148"></a>00148                                             &amp;p_bas-&gt;<a class="code" href="structble__bas__s.html#a554415e857e1b95eafd8cd7bc70d88e9">battery_level_handles</a>);
<a name="l00149"></a>00149     <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00150"></a>00150     {
<a name="l00151"></a>00151         <span class="keywordflow">return</span> err_code;
<a name="l00152"></a>00152     }
<a name="l00153"></a>00153     
<a name="l00154"></a>00154     <span class="keywordflow">if</span> (p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ac3ce984f1c066cfc0911e98fa1acdc8e">p_report_ref</a> != NULL)
<a name="l00155"></a>00155     {
<a name="l00156"></a>00156         <span class="comment">// Add Report Reference descriptor</span>
<a name="l00157"></a>00157         <a class="code" href="group__ble__types.html#gac4478362651889d2ade8030f53713588" title="Set .type and .uuid fields of ble_uuid_struct to specified uuid value.">BLE_UUID_BLE_ASSIGN</a>(ble_uuid, <a class="code" href="group___u_u_i_d___c_h_a_r_a_c_t_e_r_i_s_t_i_c_s.html#gaee4c1d5c54af835b686f10af4e630274">BLE_UUID_REPORT_REF_DESCR</a>);
<a name="l00158"></a>00158         
<a name="l00159"></a>00159         memset(&amp;attr_md, 0, <span class="keyword">sizeof</span>(attr_md));
<a name="l00160"></a>00160 
<a name="l00161"></a>00161         attr_md.<a class="code" href="structble__gatts__attr__md__t.html#af0352a482ca2a1305ec5941bc4af0ec2">read_perm</a> = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#a2c9724572f64316880f915bb78882de0">battery_level_report_read_perm</a>;
<a name="l00162"></a>00162         <a class="code" href="group___b_l_e___g_a_p___c_o_n_n___s_e_c___m_o_d_e___s_e_t___m_a_c_r_o_s.html#ga1cf3822f8bb11e28931afbb9179665b7" title="Set sec_mode pointed to by ptr to have no access rights.">BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS</a>(&amp;attr_md.<a class="code" href="structble__gatts__attr__md__t.html#a6060006e42afe1970aed0a8490f71445">write_perm</a>);
<a name="l00163"></a>00163 
<a name="l00164"></a>00164         attr_md.<a class="code" href="structble__gatts__attr__md__t.html#ac68fa2d9eb5f49771a80414d2c6bb690">rd_defer</a> = 0;
<a name="l00165"></a>00165         attr_md.<a class="code" href="structble__gatts__attr__md__t.html#ad44d77b2d08d2dde61c9ccbb22fa7432">wr_defer</a> = 0;
<a name="l00166"></a>00166         attr_md.<a class="code" href="structble__gatts__attr__md__t.html#a248864ad6fe6949aa81d376cdd7d07b4">vlen</a>     = 0;
<a name="l00167"></a>00167         
<a name="l00168"></a>00168         init_len = <a class="code" href="group__ble__sdk__srv__common.html#gaebc3fc5316f1a6a67e830516b8f9aed5" title="Encode a Report Reference Descriptor.">ble_srv_report_ref_encode</a>(encoded_report_ref, p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#ac3ce984f1c066cfc0911e98fa1acdc8e">p_report_ref</a>);
<a name="l00169"></a>00169         
<a name="l00170"></a>00170         memset(&amp;attr_char_value, 0, <span class="keyword">sizeof</span>(attr_char_value));
<a name="l00171"></a>00171 
<a name="l00172"></a>00172         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a8af1eafb75d1d1e9c4cacf188fad299e">p_uuid</a>       = &amp;ble_uuid;
<a name="l00173"></a>00173         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a573caddb6f66206646fcf38fa7c1a211">p_attr_md</a>    = &amp;attr_md;
<a name="l00174"></a>00174         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a9e68f6d248f0c128406979df16f88255">init_len</a>     = init_len;
<a name="l00175"></a>00175         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a44cd93c5a44c06d426920c36a3971eb1">init_offs</a>    = 0;
<a name="l00176"></a>00176         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a1a6c02276292f164788906e8d5a316e5">max_len</a>      = attr_char_value.<a class="code" href="structble__gatts__attr__t.html#a9e68f6d248f0c128406979df16f88255">init_len</a>;
<a name="l00177"></a>00177         attr_char_value.<a class="code" href="structble__gatts__attr__t.html#ac439d398694461e592c0af528994705e">p_init_value</a> = encoded_report_ref;
<a name="l00178"></a>00178         
<a name="l00179"></a>00179         err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga4bb7f2d804e7416d47ba64f434ac1243" title="Add a descriptor to the local server database.">ble_gatts_descriptor_add</a>(p_bas-&gt;<a class="code" href="structble__bas__s.html#a554415e857e1b95eafd8cd7bc70d88e9">battery_level_handles</a>.<a class="code" href="structble__gatts__char__handles__t.html#a61c5d051b6f79d08236fae6cf7019e75">value_handle</a>,
<a name="l00180"></a>00180                                             &amp;attr_char_value,
<a name="l00181"></a>00181                                             &amp;p_bas-&gt;<a class="code" href="structble__bas__s.html#aca91746b5636facbde7795d8825c2a0f">report_ref_handle</a>);
<a name="l00182"></a>00182         <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00183"></a>00183         {
<a name="l00184"></a>00184             <span class="keywordflow">return</span> err_code;
<a name="l00185"></a>00185         }
<a name="l00186"></a>00186     }
<a name="l00187"></a>00187     <span class="keywordflow">else</span>
<a name="l00188"></a>00188     {
<a name="l00189"></a>00189         p_bas-&gt;<a class="code" href="structble__bas__s.html#aca91746b5636facbde7795d8825c2a0f">report_ref_handle</a> = <a class="code" href="group___b_l_e___g_a_t_t_s.html#gaa16a29908fc63343706f56913be51ec1" title="Invalid Attribute Handle.">BLE_GATTS_HANDLE_INVALID</a>;
<a name="l00190"></a>00190     }
<a name="l00191"></a>00191     
<a name="l00192"></a>00192     <span class="keywordflow">return</span> <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00193"></a>00193 }
<a name="l00194"></a>00194 
<a name="l00195"></a>00195 
<a name="l00196"></a><a class="code" href="group__ble__sdk__srv__bas.html#gacb8ac9205dcec39e216575c76261c807">00196</a> uint32_t <a class="code" href="group__ble__sdk__srv__bas.html#gacb8ac9205dcec39e216575c76261c807" title="Initialize the Battery Service.">ble_bas_init</a>(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, <span class="keyword">const</span> <a class="code" href="structble__bas__init__t.html" title="Battery Service init structure. This contains all options and data needed for initialization of the s...">ble_bas_init_t</a> * p_bas_init)
<a name="l00197"></a>00197 {
<a name="l00198"></a>00198     uint32_t   err_code;
<a name="l00199"></a>00199     <a class="code" href="structble__uuid__t.html" title="Bluetooth Low Energy UUID type, encapsulates both 16-bit and 128-bit UUIDs.">ble_uuid_t</a> ble_uuid;
<a name="l00200"></a>00200 
<a name="l00201"></a>00201     <span class="comment">// Initialize service structure</span>
<a name="l00202"></a>00202     p_bas-&gt;<a class="code" href="structble__bas__s.html#a3c25f96941dd8c1b92407ed2994691c6">evt_handler</a>               = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#a3c25f96941dd8c1b92407ed2994691c6">evt_handler</a>;
<a name="l00203"></a>00203     p_bas-&gt;<a class="code" href="structble__bas__s.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a>               = <a class="code" href="group___b_l_e___c_o_n_n___h_a_n_d_l_e_s.html#gacf227b9b101dbcf08eccbbaba54e48f5">BLE_CONN_HANDLE_INVALID</a>;
<a name="l00204"></a>00204     p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a> = p_bas_init-&gt;<a class="code" href="structble__bas__init__t.html#adbf298550acd37c0d6170e90504318ac">support_notification</a>;
<a name="l00205"></a>00205     p_bas-&gt;<a class="code" href="structble__bas__s.html#a7e140bc98e49ed99a1b29c8410195884">battery_level_last</a>        = INVALID_BATTERY_LEVEL;
<a name="l00206"></a>00206     
<a name="l00207"></a>00207     <span class="comment">// Add service</span>
<a name="l00208"></a>00208     <a class="code" href="group__ble__types.html#gac4478362651889d2ade8030f53713588" title="Set .type and .uuid fields of ble_uuid_struct to specified uuid value.">BLE_UUID_BLE_ASSIGN</a>(ble_uuid, <a class="code" href="group___u_u_i_d___s_e_r_v_i_c_e_s.html#ga4090520f3007fe6f90f06a6f4f8d5845">BLE_UUID_BATTERY_SERVICE</a>);
<a name="l00209"></a>00209 
<a name="l00210"></a>00210     err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga5e8270fe6dfa01e446be17b48fe81e2a" title="Add a service declaration to the local server database.">ble_gatts_service_add</a>(<a class="code" href="group___b_l_e___g_a_t_t_s___s_v_c___t_y_p_e_s.html#ga713ed30865fa70c89587ba981f114a91">BLE_GATTS_SVC_TYPE_PRIMARY</a>, &amp;ble_uuid, &amp;p_bas-&gt;<a class="code" href="structble__bas__s.html#a363e00d9262febd8d752ed2f933be1e4">service_handle</a>);
<a name="l00211"></a>00211     <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00212"></a>00212     {
<a name="l00213"></a>00213         <span class="keywordflow">return</span> err_code;
<a name="l00214"></a>00214     }
<a name="l00215"></a>00215     
<a name="l00216"></a>00216     <span class="comment">// Add battery level characteristic</span>
<a name="l00217"></a>00217     <span class="keywordflow">return</span> battery_level_char_add(p_bas, p_bas_init);
<a name="l00218"></a>00218 }
<a name="l00219"></a>00219 
<a name="l00220"></a>00220 
<a name="l00221"></a><a class="code" href="group__ble__sdk__srv__bas.html#ga58d8f4336cce49ea5b3a2f8997bfe00e">00221</a> uint32_t <a class="code" href="group__ble__sdk__srv__bas.html#ga58d8f4336cce49ea5b3a2f8997bfe00e" title="Update battery level.">ble_bas_battery_level_update</a>(<a class="code" href="structble__bas__s.html" title="Battery Service structure. This contains various status information for the service.">ble_bas_t</a> * p_bas, uint8_t battery_level)
<a name="l00222"></a>00222 {
<a name="l00223"></a>00223     uint32_t err_code = <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>;
<a name="l00224"></a>00224 
<a name="l00225"></a>00225     <span class="keywordflow">if</span> (battery_level != p_bas-&gt;<a class="code" href="structble__bas__s.html#a7e140bc98e49ed99a1b29c8410195884">battery_level_last</a>)
<a name="l00226"></a>00226     {
<a name="l00227"></a>00227         uint16_t len = <span class="keyword">sizeof</span>(uint8_t);
<a name="l00228"></a>00228         
<a name="l00229"></a>00229         <span class="comment">// Save new battery value</span>
<a name="l00230"></a>00230         p_bas-&gt;<a class="code" href="structble__bas__s.html#a7e140bc98e49ed99a1b29c8410195884">battery_level_last</a> = battery_level;
<a name="l00231"></a>00231         
<a name="l00232"></a>00232         <span class="comment">// Update database</span>
<a name="l00233"></a>00233         err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#ga4267755972361d620ae47d791feec4a5" title="Set the value of a given attribute.">ble_gatts_value_set</a>(p_bas-&gt;<a class="code" href="structble__bas__s.html#a554415e857e1b95eafd8cd7bc70d88e9">battery_level_handles</a>.<a class="code" href="structble__gatts__char__handles__t.html#a61c5d051b6f79d08236fae6cf7019e75">value_handle</a>,
<a name="l00234"></a>00234                                        0,
<a name="l00235"></a>00235                                        &amp;len,
<a name="l00236"></a>00236                                        &amp;battery_level);
<a name="l00237"></a>00237         <span class="keywordflow">if</span> (err_code != <a class="code" href="group__nrf__error.html#ga7e6367efeaac363d8cf024940b4ec123" title="Successful command.">NRF_SUCCESS</a>)
<a name="l00238"></a>00238         {
<a name="l00239"></a>00239             <span class="keywordflow">return</span> err_code;
<a name="l00240"></a>00240         }
<a name="l00241"></a>00241         
<a name="l00242"></a>00242         <span class="comment">// Send value if connected and notifying</span>
<a name="l00243"></a>00243         <span class="keywordflow">if</span> ((p_bas-&gt;<a class="code" href="structble__bas__s.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a> != <a class="code" href="group___b_l_e___c_o_n_n___h_a_n_d_l_e_s.html#gacf227b9b101dbcf08eccbbaba54e48f5">BLE_CONN_HANDLE_INVALID</a>) &amp;&amp; p_bas-&gt;<a class="code" href="structble__bas__s.html#a36642b08bea41f0d2100e7f18fefbe10">is_notification_supported</a>)
<a name="l00244"></a>00244         {
<a name="l00245"></a>00245             <a class="code" href="structble__gatts__hvx__params__t.html" title="GATT HVx parameters.">ble_gatts_hvx_params_t</a> hvx_params;
<a name="l00246"></a>00246             
<a name="l00247"></a>00247             memset(&amp;hvx_params, 0, <span class="keyword">sizeof</span>(hvx_params));
<a name="l00248"></a>00248             len = <span class="keyword">sizeof</span>(uint8_t);
<a name="l00249"></a>00249             
<a name="l00250"></a>00250             hvx_params.<a class="code" href="structble__gatts__hvx__params__t.html#af242d6e3b6108ea75e1eb236e94c8240">handle</a>   = p_bas-&gt;<a class="code" href="structble__bas__s.html#a554415e857e1b95eafd8cd7bc70d88e9">battery_level_handles</a>.<a class="code" href="structble__gatts__char__handles__t.html#a61c5d051b6f79d08236fae6cf7019e75">value_handle</a>;
<a name="l00251"></a>00251             hvx_params.<a class="code" href="structble__gatts__hvx__params__t.html#a470560e997d7a639785b8b405de9df7d">indicate</a> = 0;
<a name="l00252"></a>00252             hvx_params.<a class="code" href="structble__gatts__hvx__params__t.html#ac681806181c80437cfab37335f62ff39">offset</a>   = 0;
<a name="l00253"></a>00253             hvx_params.<a class="code" href="structble__gatts__hvx__params__t.html#a05aad1ca0c545a05986893ca0e1ed34f">p_len</a>    = &amp;len;
<a name="l00254"></a>00254             hvx_params.<a class="code" href="structble__gatts__hvx__params__t.html#a8304c4af5da6e830b86d7199dc9a22e6">p_data</a>   = &amp;battery_level;
<a name="l00255"></a>00255             
<a name="l00256"></a>00256             err_code = <a class="code" href="group___b_l_e___g_a_t_t_s.html#gaab41731020dbcc6b9b561d8129689308" title="Notify or Indicate an attribute value.">ble_gatts_hvx</a>(p_bas-&gt;<a class="code" href="structble__bas__s.html#a0d5ffe38d68e48d81e61fc6a4999ae68">conn_handle</a>, &amp;hvx_params);
<a name="l00257"></a>00257         }
<a name="l00258"></a>00258     }
<a name="l00259"></a>00259 
<a name="l00260"></a>00260     <span class="keywordflow">return</span> err_code;
<a name="l00261"></a>00261 }
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>ble_bas.c</b>      </li>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
      Copyright &copy 2006-2011 <a href="http://www.nordicsemi.no" style="text-decoration:none">Nordic Semiconductor</a>.
      All Rights Reserved.
      <a href="disclaimer.html">Disclaimer</a>
    </li>
   </ul>
 </div>


</body>
</html>
